import omsRequest from "/utils/omsRequest";
const { showToast } = require("/utils/common");
import { createVisit } from "/api/distributeAchievement";
const { moment } = require("/utils/core");
Component({
  mixins: [],
  data: {
    wholeCountry: {}
  },
  props: {
    list: [],
    vo: {},
    oldvo: {},
    onChangeVo: (vo) => console.log(vo)
  },
  didMount() {
    // 
    let res = dd.getStorageSync({ key: 'submitTotal' });
    console.log(this.props.oldvo, "this.props.oldvo.total")
    console.log(res.data.total)
    // 回显新签商户数
    if (!this.props.oldvo.total || !res.data.total) {
      this.createVisit()
    }
    this.props.vo.total = res.data.total ? res.data.total : this.props.oldvo.total ? this.props.oldvo.total : 66 //默认值拜访数66
    this.setData({
      wholeCountry: this.props.vo
    })
  },
  didUpdate() {
  },
  didUnmount() {
    dd.setStorageSync({
      key: 'submitTotal',
      data: {
        total: this.data.wholeCountry.total
      }
    });
  },
  methods: {
    onInput(e) {
      this.setData({
        'wholeCountry.total': e.detail.value < 0 ? 0 : e.detail.value
      })
    },
    onSubmit() {
      if (!this.data.wholeCountry.total) {
        showToast({
          msg: '!注意拜访数还没进行分配'
        })
        return
      }
      this.createVisit();
    },
    onReset() {
      this.setData({
        'wholeCountry.total': null
      })
    },
    createVisit() {
      dd.showLoading()
      omsRequest({
        url: createVisit,
        method: "post",
        data: {
          total: this.data.wholeCountry.total ? this.data.wholeCountry.total : 66
        },
        headers: {
          "content-type": "application/json"
        }
      }).then((res) => {
        if (res.success) {
            dd.alert({
              content: '拜访数分配成功',
              buttonText: '确定',
              success: () => {
                this.setData({
                  'wholeCountry.distributableFlag': true // 配置成功
                })
                this.props.onChangeVo(this.data.wholeCountry)
                dd.hideLoading()
              },
            });
        }
      })
    }
  },
});
